/**
 * 修改订单
 */
var updateOrderWin = function(orderId, gridName) {
	var updateOrderForm = new Ext.form.FormPanel({
				id : 'updateOrderForm',
				frame : true,
				labelAlign : 'right',
				anchor : '100% 100%',
				layout : "form",
				buttonAlign : 'center',
				monitorValid : true,
				border : false,
				buttons : [{
							text : "保存",
							width : 60,
							formBind : true,
							handler : function() {
								updateOrderInfo(gridName);
							}
						}, {
							text : "取消",
							width : 60,
							handler : function() {
								updateOrderWindow.close();
							}
						}],
				reader : new Ext.data.JsonReader({
							success : 'success',
							root : 'data'
						}, [{
									name : "id"
								}, {
									name : "orderCode"
								}, {
									name : "userId"
								}, {
									name : "userPaymentId"
								}, {
									name : "orderStatus"
								}, {
									name : "payStatus"
								}, {
									name : "payType"
								},{
									name : "orderSource"
								}, {
									name : "payDate"
								}, {
									name : "limitPayDate"
								}, {
									name : "totalAmount"
								}, {
									name : "goodsNumber"
								}, {
									name : "payAmount"
								}, {
									name : "deliveryDate"
								}, {
									name : "deliveryTimes"
								}, {
									name : "deliveryFee"
								}, {
									name : "isUserCoupon"
								}, {
									name : "couponId"
								}, {
									name : "couponFee"
								}, {
									name : "consignee"
								}, {
									name : "consignPhone"
								}, {
									name : "consignAddress"
								}, {
									name : "birthdayTitle"
								}, {
									name : "birthdayCard"
								}, {
									name : "remark"
								}, {
									name : "payGoodsName"
								}, {
									name : "cancelDate"
								}, {
									name : "cancelReason"
								}, {
									name : "cancelName"
								}, {
									name : "receivedDate"
								}]),
				items : [{
							xtype : "hidden",
							id : 'orderForm_orgId',
							name : "id"
						}, {
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .60,
										layout : 'form',
										border : false,
										items : [{
													xtype : "displayfield",
													id : 'orderForm_orderCode',
													fieldLabel : "订单编码",
													ctCls : 'zhaospaceCSS3',
													anchor : '100%',
													name : "orderCode"
												}]
									}, {
										columnWidth : .35,
										layout : 'form',
										border : false,
										items : [{
													xtype : "displayfield",
													id : 'orderForm_orderStatus',
													fieldLabel : "订单状态",
													ctCls : 'zhaospaceCSS3',
													anchor : '100%'
												}]
									}]
						}, {
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .50,
										layout : 'form',
										border : false,
										items : [{
													xtype : "textfield",
													fieldLabel : "<span style='color:#F00'>*</span>收货人姓名",
													anchor : '100%',
													allowBlank:false,
													name : "consignee"
												}]
									}, {
										columnWidth : .50,
										layout : 'form',
										border : false,
										items : [{
													xtype : "textfield",
													fieldLabel : "<span style='color:#F00'>*</span>收货人电话",
													anchor : '100%',
													allowBlank:false,
													name : "consignPhone"
												}]
									}]
						}, {
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .50,
										layout : 'form',
										border : false,
										items : [{
													xtype : "datefield",
													id : 'orderForm_deliveryDate',
													name : "deliveryDate",
													fieldLabel : "<span style='color:#F00'>*</span>配送日期",
													anchor : '100%',
													allowBlank:false,
													editable : false,
													format : "Y-m-d",// 日期的格式
													minValue : new Date(),
													maxValue:getDeliverymMaxDate()
												}]
									}, {
										columnWidth : .50,
										layout : 'form',
										border : false,
										items : [{
											xtype : "combo",
											fieldLabel : "<span style='color:#F00'>*</span>配送时间段",
											anchor : '100%',
											editable:false,
											allowBlank:false,
											hiddenName : "deliveryTimes",
											emptyText : "选择状态",
											store : new Ext.data.ArrayStore({
														fields : ["value",
																"text"],
														data : [
																['10:00~12:00',
																		"10:00~12:00"],
																['12:00~14:00',
																		"12:00~14:00"],
																['14:00~16:00',
																		"14:00~16:00"],
																['16:00~18:00',
																		"16:00~18:00"],
																['18:00~20:00',
																		"18:00~20:00"]]
													}),
											valueField : "value",
											displayField : "text",
											mode : "local",
											triggerAction : "all"
										}]
									}]
						}, {
							layout : 'column',
							border : false,
							items : [{
										columnWidth : 1,
										layout : 'form',
										border : false,
										items : [{
													xtype : "textfield",
													fieldLabel : "<span style='color:#F00'>*</span>收货地址",
													anchor : '100%',
													allowBlank:false,
													name : "consignAddress"
												}]
									}]
						}, {

							layout : 'column',
							border : false,
							items : [{
										columnWidth : 1,
										layout : 'form',
										border : false,
										items : [{
													xtype : "textarea",
													fieldLabel : "备注",
													anchor : '100%',
													name : "remark",
													height : 100,
													vtype : "validateMaxLength",
													confirmTo : 400
												}]
									}]

						}]
			});

	var updateOrderWindow = new Ext.Window({
				id : 'updateOrderWindow',
				closeAction : 'close',
				border : false,
				title : '订单信息',
				modal : true,
				width : 500,
				height : 300,
				plain : true,
				layout : 'fit',
				buttonAlign : 'center',
				autoDestroy : true,
				items : [updateOrderForm],
				listeners : {
					'show' : function() {
						lookOrderById(orderId);
					},
					'beforeshow' : function() {
						updateOrderWindow.center();
					}
				}
			});
	return updateOrderWindow;
}

/**
 * 根据orderId查询改orderModel对象
 * 
 * @param {}
 *            orderId
 */
function lookOrderById(orderId) {
	if (orderId != null && orderId != "undefined") {
		Ext.getCmp("updateOrderForm").getForm().load({
			deferredRender : false,
			url : __ctxPath + "/orderManager/findOrderById",
			params : {
				orderId : orderId
			},
			waitTitle : "请稍候",
			waitMsg : "正在载入数据...",
			root : "data",
			success : function(response, options) {
				var data = options.result.data;
				Ext.getCmp('orderForm_orderStatus').setValue(PmsUtil
						.OrderStatus(data.orderStatus));
				// 配送时间格式设置
				var deliveryDate = new Date(Date.parse(data.deliveryDate
						.replace(/-/g, "/")));
				Ext.getCmp("orderForm_deliveryDate").setValue(deliveryDate
						.format('Y-m-d'));
			},
			failure : function(f, g) {
				Ext.MessageBox.show({
							title : "操作信息",
							msg : "数据加载失败!",
							buttons : Ext.MessageBox.OK,
							icon : Ext.MessageBox.ERROR
						});
			}
		});
	}
};
/**
 * 修改订单信息
 */
function updateOrderInfo(gridName) {
	var b = Ext.getCmp("updateOrderForm");
	if (b.getForm().isValid()) {
		Ext.Msg.confirm("提示框", "确认要保存订单信息吗?", function(button) {
					if (button == "yes") {
						b.getForm().submit({
									url : __ctxPath
											+ "/orderManager/updateOrder",
									method : "post",
									waitTitle : "请稍候",
									waitMsg : "正在提交表单，请稍候......",
									success : function(c, d) {
										var jsonData = Ext.util.JSON
											.decode(d.response.responseText);
										Ext.MessageBox.show({
													title : "操作信息",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.INFO
												});
										Ext.getCmp("updateOrderWindow").close();
										var gridPanel = Ext.getCmp(gridName);
										if (typeof(gridPanel) != 'undefined') {
											gridPanel.store.reload();
										}
									},
									failure : function(c, d) {
										var jsonData = Ext.util.JSON
											.decode(d.response.responseText);
										Ext.MessageBox.show({
													title : "操作信息",
													msg : jsonData.data,
													buttons : Ext.MessageBox.OK,
													icon : Ext.MessageBox.ERROR
												});
									}
								});
					}
				})
	}
};
/**
 * 获取当前日期以后日期
 */
function getDeliverymMaxDate(){
	var startTime=new Date();
	startTime.setDate(startTime.getDate() + 30);
	return new Date(startTime).format('Y-m-d');
}
